package com.mangowang.module.main.ui;


import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
import com.google.protobuf.InvalidProtocolBufferException;
import com.mangowang.lib.common.base.BaseActivity;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.MemberOuterClass;
import com.mangowang.lib.common.proto.Transfer;
import com.mangowang.lib.common.util.StatusBarUtil;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AccountNewActivity extends BaseActivity {

    Unbinder unbinder;
    @BindView(R2.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R2.id.view_pager)
    ViewPager viewPager;
    @BindView(R2.id.back_iv)
    ImageView backIv;
    @BindView(R2.id.tv_title)
    TextView tvTitle;


    private List<Fragment> fragmentList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_new);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#FFFFFF"), true);
        unbinder = ButterKnife.bind(this);
        backIv.setOnClickListener(v -> finish());
        initView();
    }



    private void initView() {

        tvTitle.setText("我的账户");


        // 获取分类

        NetworkManager.INSTANCE.post(Apis.get_patterns, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Transfer.pattern_list data = Transfer.pattern_list.parseFrom(response);
                List<Transfer.pattern> lists = data.getListList();
                Log.e("patterns",lists.toString());
                List<String> titles = new ArrayList<>();
                fragmentList = new ArrayList<>();
                if(lists.size()<=1) tabLayout.setVisibility(View.GONE);

                for (int i = 0 ;i< lists.size();i++)
                {
                    titles.add(lists.get(i).getName());
                    fragmentList.add(
                            AccountNewFragment.NewInstance(Integer.parseInt(lists.get(i).getCode()),lists.get(i).getName(),i));
                }


                viewPager.setOffscreenPageLimit(lists.size());
                viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
                    @Override
                    public Fragment getItem(int position) {
                        return fragmentList.get(position);
                    }

                    @Override
                    public int getCount() {
                        return fragmentList.size();
                    }
                    @Override
                    public CharSequence getPageTitle(int position) {
                        return titles.get(position);
                    }

                });
                //设置图标
                tabLayout.setupWithViewPager(viewPager);
                for (int i = 0; i < titles.size(); i++) {
                    tabLayout.getTabAt(i).setCustomView(getTabView(titles.get(i)));
                }

            }

            @Override
            public void onError(int errorCode, String errorMessage) {
//                showToast(errorMessage);
            }
        });
    }


    public View getTabView(String tabName) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.tab_select_layout, null);
        TextView tv_tab = view.findViewById(R.id.tv_tab);
        tv_tab.setText(tabName);
        return view;
    }
}